package app.fugema.shop.main.evaluate.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import app.fugema.shop.R;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.connector.BaseItemMultiType;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.viewmodel.BaseViewObserver;

import app.fugema.shop.constants.PublicGlobal;
import app.fugema.shop.databinding.ActivityProductQuestionDetailBinding;

import app.fugema.shop.main.evaluate.adapter.QuestionAnswerAdapter;
import app.fugema.shop.main.evaluate.model.AnswerResult;
import app.fugema.shop.main.evaluate.model.QusetionHeadBean;
import app.fugema.shop.main.evaluate.model.UserAnswerBean;
import app.fugema.shop.main.evaluate.vm.EvaluateVM;
import app.fugema.shop.main.login.model.MUserInfo;
import app.fugema.shop.util.SimpleTextWatcher;


public class QuestionDetailActivity extends BaseTitleActivity<ActivityProductQuestionDetailBinding, EvaluateVM>
{

    public static void start(Context context, int questionId)
    {
        Intent starter = new Intent(context,QuestionDetailActivity.class);
        starter.putExtra("qId", questionId);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_question_detail);
    }

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }


    private QuestionAnswerAdapter adapter;
    private int questionId ;
    private MUserInfo mUser;
    private QusetionHeadBean qusetHead;
    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("问问大家",R.color.white);

        adapter = new QuestionAnswerAdapter();

        //adapter.setTypeFixed(BaseItemMultiType.TYPE_GROUP);
        binding.rvRecyclerView.setAdapter(adapter);
        //LiveEventBus.get(AppConstants.EventKey.PET_SELECT, Object.class).post(bean);
        binding.etAnswer.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(String content)
            {

            }
        });

        //点击 提交回答
        binding.btnApplyAnswer.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view)
            {

                if( UIUtils.isEmpty( mUser ) )
                {
                    UIUtils.showToastSafes("登录");
                    return;
                }

                String answerStr = binding.etAnswer.getText().toString();

                viewModel.applyAnswer( questionId , mUser.getUserId(),answerStr)
                         .observe(QuestionDetailActivity.this, new BaseViewObserver<LiveDataWrapper<BaseApiResult>>()
                         {
                             @Override
                             protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                             {
                                    if( data.data.isSuccess() )
                                    {
                                        UIUtils.showToastSafes( data.data.getMsg() );
                                        binding.etAnswer.setText("");
                                        queryQA(questionId);
                                    }
                             }
                         });
            }
        });
    }

    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);
        questionId = intent.getIntExtra("qId",0);
        if( LoginUtils.isLogin() )
        {
            mUser = PublicGlobal.getUserInfo();
        }

        queryQA(questionId);
    }


    private void queryQA(int qId)
    {
        viewModel.getQuestionDetailById( qId )
                .observe(this, new BaseViewObserver<LiveDataWrapper<AnswerResult>>(this)
                {
                    @Override
                    protected void onSuccess(LiveDataWrapper<AnswerResult> data)
                    {
                        if( UIUtils.isNotEmpty( data.data.getData() ) && !data.data.getData().isEmpty() )
                        {
                            UserAnswerBean answerBean = data.data.getData().get(0);
                            qusetHead = new QusetionHeadBean();
                            qusetHead.setItemType(BaseItemMultiType.TYPE_HEAD );
                            qusetHead.setProductName( answerBean.getProductName() );
                            qusetHead.setProductImg( answerBean.getImagePathUrl() );
                            adapter.addHeaderView( qusetHead ,0 );
                            adapter.setDataList( data.data.getData() );
                        }
                    }
                });
    }


        /*
        new SideBarSortUtils(binding.rvRecycler, binding.sidebar)
{
    @Override
    public int getWordPosition(String word) {
    for (int i = 0; i < adapter.getItemCount(); i++) {
        if (adapter.getItemBean(i) instanceof Object) {
            Object allBean = (Object) adapter.getItemBean(i);
            if (allBean.equals(word)) {
                return i;
            }
        }
    }
    return 0;
}

    @Override
    public String getWordByPosition(int position) {
    if (position > -1 && position < adapter.getItemCount()) {
        if (adapter.getItemBean(position) instanceof Object) {
            Object allBean = (Object) adapter.getItemBean(position);
            return allBean.toString();
        }
    }
    return null;
}
};
*/


}
